package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.cskaoyan.mapper.IndexMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.service
 * @date 2020/12/10 21:52
 * @Copyright © 暂时还没有
 * @description
 */
@Service
public class AdminGrouponServiceImp implements AdminGrouponService{

    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    IndexMapper indexMapper;
    @Override
    public ListData<GrouponRules> grouponList(Integer goodsId, Integer page, Integer limit, String sort, String order) {

        PageHelper.startPage(page,limit);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();

        if (goodsId != null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        ListData<GrouponRules>listData = new ListData<>();
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        listData.setItems(grouponRules);
        PageInfo pageInfo = new PageInfo(grouponRules);
        long total = pageInfo.getTotal();
        listData.setTotal((int)total);

        return listData;
    }

    @Override
    public void grouponUpdate(GrouponRules grouponRules) {

        grouponRules.setUpdateTime(TimeTool.getCurrentTime());
        grouponRulesMapper.updateByPrimaryKey(grouponRules);

    }

    @Override
    public GrouponRules grouponInsert(Integer goodsId, BigDecimal discount, Integer discountMember, Date expireTime) {

        GrouponRules grouponRules = new GrouponRules();
        grouponRules.setGoodsId(goodsId);
        grouponRules.setDiscount(discount);
        grouponRules.setDiscountMember(discountMember);
        grouponRules.setExpireTime(expireTime);
        grouponRules.setAddTime(TimeTool.getCurrentTime());
        grouponRules.setUpdateTime(TimeTool.getCurrentTime());
        grouponRules.setDeleted(false);

        //通过goodsId在goods表中查询pic_url,goods_name
        Goods good = goodsMapper.selectByPrimaryKey(goodsId);
        grouponRules.setGoodsName(good.getName());
        grouponRules.setPicUrl(good.getPicUrl());

        grouponRulesMapper.insert(grouponRules);
        GrouponRules grouponRulesNew = grouponRulesMapper.selectByPrimaryKey(grouponRules.getId());

        return grouponRulesNew;
    }

    @Override
    public void grouponDelete(GrouponRules grouponRules) {
        grouponRulesMapper.deleteByPrimaryKey(grouponRules.getId());
        GrouponRules grouponRulesDeleted = grouponRulesMapper.selectByPrimaryKey(grouponRules.getId());
        System.out.println(grouponRulesDeleted);
        if (grouponRulesDeleted != null){
            throw new IllegalStateException("删除不成功");
        }
    }

    @Override
    public ListData<Groupon> grouponListRecord
            (Integer goodsId, Integer page, Integer limit, String sort, String order) {

        ListData<Groupon> listData = new ListData();
        //删除过时活动,以现在时间为准
        indexMapper.deleteOverTimeGroupon(TimeTool.getCurrentTime());
        //判断人数，时间
        int count = indexMapper.queryCountGroupon(goodsId);

        List<Groupon> groupons = indexMapper.queryGrouponRecord(goodsId, page-1, limit, sort, order);

        listData.setItems(groupons);
        listData.setTotal(groupons.size());
        return listData;
    }
}
